/*
* 会话模块 
*/

'use strict'

YX.fn.session = function () {
    //搜索当前会话列表
	this.$searchSessionChat = $('#searchSessionChat')
	this.$searchSessionChat.bind("input propertychange",this.searchSessionChat.bind(this))
    this.$searcSessionList = $('#searcSessionList')
	// this.$searchTeamAndCaseList.blur(this.searchTeamAndCaseListBlur.bind(this))
	// this.$searchTeamAndCaseList.focus(this.searchTeamAndCaseListFocus.bind(this))
	this.$searcSessionList.delegate('li','click',this.showChatBox.bind(this))
    this.$searchImgIcon = $("#searchImgIcon");
    this.$searchImgIcon.on("click",this.searchImgIconFun.bind(this))
}
/**
 * 最近联系人显示
 * @return {void}
 */
YX.fn.buildSessions = function(id) {
    var data = {
        sessions:this.cache.getSessions(),
        personSubscribes: this.cache.getPersonSubscribes()
    }
    // console.log("获取最近联系人=",data)
    // console.log("最近的session=",this.sessions)
    if(!this.sessions){
        var options = {
            data:data,
            // onclickavatar:this.showInfo.bind(this),
            onclickitem:this.openChatBox.bind(this),
            infoprovider:this.infoProvider.bind(this),

        } 
        this.sessions = new NIMUIKit.SessionList(options)
        this.sessions.inject($('#sessions').get(0))
    }else{
        this.sessions.update(data)
    }
    //导航上加未读示例  
    this.showUnread()         		
    this.doPoint()
    //已读回执处理
    this.markMsgRead(id)
    var $node = $(".m-unread .u-unread")
    $node.on('mouseenter', function () {
        $node.text('×')
    })
    $node.on('mouseleave', function () {
        $node.text(this.totalUnread)
    }.bind(this))
    $node.on('click', function (event) {
        this.nim.resetAllSessionUnread()
        event.preventDefault()
    }.bind(this))
}
 // 导航上加未读数
YX.fn.showUnread = function () {
    var counts = $("#sessions .panel_count")
    this.totalUnread = 0
    if(counts.length!==0){
        if(this.totalUnread !=="99+"){
            for (var i = counts.length - 1; i >= 0; i--) {
                if($(counts[i]).text()==="99+"){
                    this.totalUnread = "99+"
                    break
                }
                this.totalUnread +=parseInt($(counts[i]).text(),10)
            }
        }
    }
    var $node = $(".m-unread .u-unread")
    $node.text(this.totalUnread)
    this.totalUnread?$node.removeClass("hide"):$node.addClass("hide")
}

//搜索输入框触发
YX.fn.searchSessionChat = function(e){
    var $input = $('#searchSessionChat'),
		name = $input.val().trim(),
		that = this,
		searchList = new Array();
	var sessionList = this.cache.getSessions();
	
    
	//根据输入框值判断是用teamId还是群组名称进行筛选
	if(!name){
		$('#searchSessionChat').val("")
		// this.$searcSessionList.html("");
        this.$searcSessionList.addClass("hide")
        this.$searchImgIcon.addClass("hide");
		return;
	}
    

    for(let i = 0;i<sessionList.length;i++){
        if(sessionList[i].scene == 'p2p' &&(this.getNick(sessionList[i].lastMsg.target)).indexOf(name) != -1){
            searchList.push(sessionList[i])
        }
    }
   
	this.$searchImgIcon.removeClass("hide");
	this.setSearchSessionChatNode(searchList,this);
}

//显示html节点
YX.fn.setSearchSessionChatNode = (list,_that)=>{
    
    let html = [];
    list.forEach(item =>{
        let nick = "",avatar="",msg =item.lastMsg;
        if (msg.target === userUID) {
            nick = '我的手机';
            avatar = './images/myPhone.png';
          } else {
            var userInfo = _that.cache.getUserById(msg.target);
            nick = _that.getNick(msg.target);
            avatar = getAvatar(userInfo.avatar);
          }
        html.push(`
            <li data-target=${msg.target}>
                <img src="${avatar}" alt="头像">
                <span>${nick}</span>
            </li>
        `)
    })
    if(html.length == 0){
        html.push(`
            <li>
                <span>找不到匹配的结果</span>
            </li>
        `)
      
    }
    _that.$searcSessionList.removeClass("hide")
    _that.$searcSessionList.html(html.join(""));
}

//打开聊天版
YX.fn.showChatBox = function(e){
    var account = e.currentTarget.dataset.target;
	this.openChatBox(account,'p2p');
    this.$searchSessionChat.val("");
    this.$searcSessionList.addClass("hide")
    this.$searchImgIcon.addClass("hide");
}

//清空聊天
YX.fn.searchImgIconFun = function(e){
    this.$searcSessionList.addClass("hide")
    this.$searchImgIcon.addClass("hide");
    this.$searchSessionChat.val("")
}